<?php
/**
 * This is the model class for table "{{profile}}".
 *
 * The followings are the available columns in table "{{profile}}":
 * @property integer $id
 * @property integer $related_id
 * @property string $language
 * @property string $full_name
 * @property string $birth_date
 * @property integer $birth_place
 * @property integer $sex
 * @property integer $age
 * @property string $address
 * @property integer $district_id
 * @property string $phone_number
 * @property string $status
 * @property integer $height
 * @property integer $height_unit
 * @property integer $weight
 * @property integer $weight_unit
 * @property integer $do_drink_alcohol
 * @property integer $do_smoke
 * @property integer $right_eye
 * @property integer $left_eye
 * @property integer $main_hand
 * @property string $blood_group
 * @property string $photo_url
 * @property string $favorite
 * @property string $sport
 * @property string $advantage
 * @property string $disadvantage
 * @property string $purpose_of_go_abroad
 * @property string $skill_to_improve
 * @property string $after_go_abroad
 * @property string $has_friend_and_relative
 * @property string $submitter
 * @property string $translator
 * @property string $translate_date
 * @property string $acceptor
 * @property string $accept_date
 * @property int $is_deleted
 * @property string $update_date
 * @property stirng $updated_by
 * @property string $create_date
 * @property string $created_by
 * @property string $is_pass_health_checking
 * @property string $type_of_power
 * @property string $health_checking_date
 * @property string $type_of_health
 * @property string $do_forward_profile
 * @property string $updated_fields
 * The followings are the available model relations:
 * @property Attachment[] $attachments
 * @property District $district
 * @property Family $family
 * @property Academy $academy
 * @property WorkingProcess[] $workingProcesses
 * @property Province $province
 * @property Profile $profileJP
 * @property AdvanceProfile $advanceProfile
 */
class Profile extends CActiveRecord {
	/**
	 * It's includes fullname or name which are information for search function
	 * 
	 * @var type String
	 */
	private $beforeUpdateProfile;
	public $keyword;
	public $birth_year;
	public $month_of_flight;
	public $year_of_flight;
	public $order;
	const SCENARIO_SEARCH = 'search';
	const SCENARIO_CREATE = 'create';
	const SCENARIO_UPDATE = 'update';
	const SCENARIO_EXPORT = 'export';
	
	/**
	 * Returns the static model of the specified AR class.
	 * 
	 * @return profile the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model ( $className );
	}
	
	/**
	 *
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{profile}}';
	}
	
	/**
	 *
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array (
				array (
						'birth_date, full_name, birth_place, sex, phone_number, address',
						'required',
						'message' => t ( 'MSG_REQUIRED', 'messages' ) 
				),
				array (
						'do_forward_profile, is_pass_health_checking, main_hand, height, weight, age, right_eye, left_eye, do_drink_alcohol, do_smoke',
						'numerical',
						'integerOnly' => true,
						'message' => t ( 'MSG_WRONG_NUMBER', 'messages' ) 
				),
				array (
						'full_name, favorite, sport, skill_to_improve, has_friend_and_relative, type_of_power, type_of_health',
						'length',
						'max' => '50',
						'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' ) 
				),
				array (
						'language, blood_group',
						'length',
						'max' => '2',
						'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' ) 
				),
				array (
						'phone_number',
						'length',
						'max' => '200',
						'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' ) 
				),
				array (
						'address, photo_url, advantage, disadvantage, purpose_of_go_abroad, after_go_abroad',
						'length',
						'max' => '200',
						'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' ) 
				),
				array (
						'create_date, update_date, health_checking_date',
						'safe' 
				),
				// The following rule is used by search().
				// Please remove those attributes that should not be searched.
				array (
						'status, hiring_company_id, introducing_company_id, union_id, is_pass_health_checking, type_of_health, do_forward_profile, type_of_power, birth_date, is_deleted, keyword, age, birth_year, full_name, address, phone_number, sex, birth_place, married, month_of_flight, year_of_flight, order',
						'safe',
						'on' => 'search' 
				),
				array (
						'keyword',
						'length',
						'max' => 100,
						'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' ) 
				),
				array (
						'full_name',
						'length',
						'min' => '3',
						'tooShort' => t ( 'MSG_STRING_TOO_SHORT', 'messages' ),
						'max' => '50',
						'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' ) 
				) 
		);
	}
	
	/**
	 *
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array (
				'advanceProfile' => array (
						self::HAS_ONE,
						'AdvanceProfile',
						'profile_id'
				),
				'family' => array (
						self::HAS_ONE,
						'Family',
						'profile_id' 
				),
				'academy' => array (
						self::HAS_ONE,
						'Academy',
						'profile_id' 
				),
				'attachments' => array (
						self::HAS_MANY,
						'Attachment',
						'profile_id'
				),
				'workingProcesses' => array (
						self::HAS_MANY,
						'WorkingProcess',
						'profile_id' 
				),
				'province' => array (
						self::BELONGS_TO,
						'Province',
						'birth_place'
				),
				'profileJP' => array (
						self::HAS_MANY,
						'Profile',
						'related_id'
				),
				'profileVN' => array (
						self::BELONGS_TO,
						'Profile',
						'id'
				)
		);
	}
	
	/**
	 *
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array (
				'id' => t ( 'LBL_ID', 'profile' ),
				'language' => 'Language',
				'related_id' => 'Related',
				'full_name' => t ( 'LBL_FULL_NAME', 'profile' ),
				'birth_date' => t ( 'LBL_BIRTH_DATE', 'profile' ),
				'birth_place' => t ( 'LBL_BIRTH_PLACE', 'profile' ),
				'sex' => t ( 'LBL_SEX', 'profile' ),
				'age' => t ( 'LBL_AGE', 'profile' ),
				'update_date' => t ( 'LBL_UPDATE_DATE', 'profile' ),
				'create_date' => t ( 'LBL_CREATE_DATE', 'profile' ),
				'updated_by' => t ( 'LBL_UPDATED_BY', 'profile' ),
				'created_by' => t ( 'LBL_CREATED_BY', 'profile' ),
				'address' => t ( 'LBL_ADDRESS', 'profile' ),
				'district_id' => t ( 'LBL_DISTRICT', 'profile' ),
				'phone_number' => t ( 'LBL_PHONE_NUMBER', 'profile' ),
				'status' => t ( 'LBL_STATUS', 'profile' ),
				'photo_url' => t ( 'LBL_PHOTO_URL', 'profile' ),
				'height' => t ( 'LBL_HEIGHT', 'profile' ),
				'weight' => t ( 'LBL_WEIGHT', 'profile' ),
				'do_drink_alcohol' => t ( 'LBL_DO_DRINK_ALCOHOL', 'profile' ),
				'do_smoke' => t ( 'LBL_DO_SMOKE', 'profile' ),
				'right_eye' => t ( 'LBL_RIGHT_EYE', 'profile' ),
				'left_eye' => t ( 'LBL_LEFT_EYE', 'profile' ),
				'main_hand' => t ( 'LBL_MAIN_HAND', 'profile' ),
				'blood_group' => t ( 'LBL_BLOOD_GROUP', 'profile' ),
				'favorite' => t ( 'LBL_FAVORITE', 'profile' ),
				'sport' => t ( 'LBL_SPORT', 'profile' ),
				'advantage' => t ( 'LBL_ADVANTAGE', 'profile' ),
				'disadvantage' => t ( 'LBL_DISADVANTAGE', 'profile' ),
				'purpose_of_go_abroad' => t ( 'LBL_PURPOSE_OF_GO_ABROAD', 'profile' ),
				'skill_to_improve' => t ( 'LBL_SKILL_TO_IMPROVE', 'profile' ),
				'after_go_abroad' => t ( 'LBL_AFTER_GO_ABROAD', 'profile' ),
				'has_friend_and_relative' => t ( 'LBL_HAS_FRIEND_AND_RELATIVE', 'profile' ),
				'dayOfBirth' => t ( 'LBL_DAY', 'profile' ),
				'monthOfBirth' => t ( 'LBL_MONTH', 'profile' ),
				'yearOfBirth' => t ( 'LBL_YEAR', 'profile' ),
				'married' => t ( 'LBL_MARRIED', 'family' ),
				'hiring_company_id' => t ( 'LBL_HIRING_COMPANY_ID', 'advanceProfile' ),
				'submitter' => t ( 'LBL_CREATED_BY', 'profile' ), 
				'is_pass_health_checking' => t ( 'LBL_IS_PASS_HEALTH_CHECKING', 'profile' ),
				'type_of_power' => t ( 'LBL_TYPE_OF_POWER', 'profile' ),
				'health_checking_date' => t ( 'LBL_HEALTH_CHECKING_DATE', 'profile' ),
				'type_of_health' => t ( 'LBL_TYPE_OF_HEALTH', 'profile' ),
				'do_forward_profile' => t ( 'LBL_DO_FORWARD_PROFILE', 'profile' ),
				'month_of_flight' => t ( 'LBL_MONTH_OF_FLIGHT', 'profile' ),
				'year_of_flight' => t ( 'LBL_YEAR_OF_FLIGHT', 'profile' ),
				'order' => t('LBL_ORDER', 'advanceProfile')
		);
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * 
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$this->keyword = trim ( $this->keyword );
		$criteria = new CDbCriteria ();		
		
		$criteria->addSearchCondition ( 't.full_name', $this->keyword, true,'OR' );
		$criteria->addSearchCondition ( 't.phone_number', $this->keyword, true, 'OR' );
		
		$criteria->compare ( 't.is_deleted', 0 ); // filter out only is_deleted = 0;
		$criteria->compare ( 'sex', $this->sex );
		$criteria->compare ( 'birth_place', $this->birth_place );
		$criteria->compare ( 'status', $this->status );
		// Advanced search

		$criteria->compare ( 'YEAR(birth_date)', $this->birth_year);
		$criteria->compare ( 'YEAR(go_date)', $this->year_of_flight);
		$criteria->compare ( 'MONTH(go_date)', $this->month_of_flight);
		$criteria->compare ( '`order`', $this->order, true);
		$criteria->compare ( 'age', $this->age );
		$criteria->compare ( 'height', $this->height );
		$criteria->compare ( 'weight', $this->weight );
		$criteria->compare ( 'left_eye', $this->left_eye );
		$criteria->compare ( 'right_eye', $this->right_eye );
		$criteria->compare ( 'blood_group', $this->blood_group );
		$criteria->compare ( 'do_drink_alcohol', $this->do_drink_alcohol );
		$criteria->compare ( 'do_smoke', $this->do_smoke );
		$criteria->compare ( 't.language', ProfileLanguage::VN );
		
		// detect submitter
		$currentUser = User::model ()->findByPk ( Yii::app ()->user->id );
		if ($currentUser->isSubmitter ()) {
			$criteria->compare ( 'submitter', $currentUser->username );
		}
		$criteria->with = array (
				'family' 
		);
		$criteria->with = (array('advanceProfile' => array('with' => array('hiringCompany'))));
		if ($this->family != null) {
			$criteria->compare ( 'married', $this->family->married );
		}
		if ($this->advanceProfile != null) {
			$criteria->compare ( 'hiring_company_id', $this->advanceProfile->hiring_company_id);
			$criteria->compare ( 'introducing_company_id', $this->advanceProfile->introducing_company_id);
			if($this->advanceProfile->hiringCompany != null)
			$criteria->compare ( 'union_id', $this->advanceProfile->hiringCompany->union_id);
		}
		
		$sort = new CSort ();
		$sort->defaultOrder = array (
				'id' => CSort::SORT_DESC 
		);
		$sort->attributes = array (
				'*' 
		);
		
		return new CActiveDataProvider ( $this, array (
				'criteria' => $criteria,
				'pagination' => array (
						'pageSize' => LGHelpers::getPageSize ( $this->count () ) 
				),
				'sort' => $sort 
		) );
	}
	
	protected function afterFind() {
		$this->update_date = strtotime ( $this->update_date );
		$this->update_date = date ( LGConstant::DATE_FORMAT, $this->update_date );
		
		$this->birth_date = strtotime ( $this->birth_date );
		$this->birth_date = date ( LGConstant::DATE_FORMAT_NOT_TIME_MONTH_NUMBER, $this->birth_date );
		
		if(isset($this->health_checking_date) && $this->health_checking_date != NULL){
			$this->health_checking_date = strtotime ( $this->health_checking_date );
			$this->health_checking_date = date ( LGConstant::DATE_FORMAT_NOT_TIME_MONTH_NUMBER, $this->health_checking_date );
		}
		
		if ($this->family == null) {
			$this->family = new Family ();
		}
		
		if ($this->academy == null) {
			$this->academy = new Academy ();
		}
		
		if ($this->advanceProfile == null) {
			$this->advanceProfile = new AdvanceProfile();
		}
		
		if ($this->advanceProfile->hiringCompany == null){
			$this->advanceProfile->hiringCompany  = new HiringCompany();
		}
		
		if($this->advanceProfile->hiringCompany->union == NULL){
			$this->advanceProfile->hiringCompany->union = new Union();
		}
		
		if ($this->advanceProfile->introducingCompany == null)
			$this->advanceProfile->introducingCompany = new IntroducingCompany();
		
		parent::afterFind ();
	}
	public function beforeSave() {
		if (parent::beforeSave ()) {
			
			$current_date = date ( LGConstant::DATE_FORMAT_FOR_DATABASE );
			// Get user is logging
			$logedInUser = User::model ()->findByPk ( Yii::app ()->user->id );
			
			if ($this->isNewRecord) {
				$this->create_date = $current_date;
				$this->created_by = $logedInUser->username;
				$this->submitter = $logedInUser->username;
			}
			
			$this->update_date = $current_date;
			if (isset ( $logedInUser )) {
				$this->updated_by = $logedInUser->username;
			}
			
			$this->age = floor((time() - strtotime($this->birth_date))/31556926);
			$date = DateTime::createFromFormat ( 'd-m-Y', $this->birth_date );
			$this->birth_date = $date->format ( 'Y-m-d' );
			
			if(isset($this->health_checking_date) && $this->health_checking_date != NULL){
				$date = DateTime::createFromFormat ( 'd-m-Y', $this->health_checking_date );
				$this->health_checking_date = $date->format ( 'Y-m-d' );
			}else{
				$this->health_checking_date = NULL;
			}
			
			$this->setIsNewRecord ( false );
			return true;
		} else {
			return false;
		}
	}
	public function export($ids) {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$ids = explode(',', $ids);
		$criteria = new CDbCriteria ();
		$criteria->addInCondition('t.id', $ids);
		$sort = new CSort ();
		$sort->defaultOrder = array (
				'id' => CSort::SORT_DESC 
		);
		$criteria->with = array (
				'academy'
		);
		$criteria->with = array (
				'province'
		);
		$criteria->with = array (
				'profileJP'
		);
		$sort->attributes = array (
				'*' 
		);
		
		$dataProvider = new CActiveDataProvider ( $this, array (
				'criteria' => $criteria,
				'sort' => $sort,
				'pagination'    => false
		) );
		return $dataProvider->data;
	}
	
	public function get_translated_model(){
		$model = null;
		$criteria = new CDbCriteria ();
   		$criteria->compare('t.related_id', $this->id);
   		$criteria->compare('t.language', 'JP');
    	$criteria->with = array (
				'family' 
		);
    	$criteria->with = array (
    			'workingProcesses'
    	);
    	$criteria->with = (array('academy' => array('with' => array('studyingProcesses'))));
		$model = $this->findAll($criteria);
		return $model;
	}
	
	public function exportAll($ids) {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$ids = explode(',', $ids);
		$criteria = new CDbCriteria ();
		$criteria->compare('t.language', 'VN');
		$criteria->addInCondition('t.id', $ids);
		$sort = new CSort ();
		$sort->defaultOrder = array (
				'id' => CSort::SORT_DESC
		);
		$criteria->with = array (
				'advanceProfile'
		);
		$criteria->with = array (
				'family'
		);
		$criteria->with = array (
				'province'
		);
		$sort->attributes = array (
				'*'
		);
	
		$dataProvider = new CActiveDataProvider ( $this, array (
				'criteria' => $criteria,
				'sort' => $sort,
				'pagination'    => false
		) );
		return $dataProvider->data;
	}
}

?>
